﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using weikebuluo.DAL;
using weikebuluo.Model;
using weikebuluo.Common;

namespace weikeweixin.web.admin.order
{
    public partial class foodadd : weikeweixin.web.admin.cs.adminbase
    {
        a_foodclass_dal foodclassdal = new a_foodclass_dal();
        a_foodlist_dal foodlistdal = new a_foodlist_dal();
        protected a_foodlist_model foodlistmodel = new a_foodlist_model();
        protected DataTable dtfoodclass = new DataTable();

        protected void Page_Load(object sender, EventArgs e)
        {
            dtfoodclass = foodclassdal.GetList("store_id="+user_id).Tables[0];
            
            if (!string.IsNullOrEmpty(Request.QueryString["id"]))
            {
                foodlistmodel = foodlistdal.GetModel(int.Parse(Request.QueryString["id"]));
            }
            switch (Request.Form["action"])
            {
                case "add":
                    add();
                    break;
            }
        }
        private void add()
        {
            if (!string.IsNullOrEmpty(Request.Form["id"]))
            {
                foodlistmodel = foodlistdal.GetModel(int.Parse(Request.Form["id"]));
            }
            foodlistmodel.foodlist_name = Request.Form["foodlist_name"].Trim().ToString();
            foodlistmodel.foodlist_price = decimal.Parse(Request.Form["foodlist_price"].Trim().ToString());
            foodlistmodel.foodlist_picurl = Request.Form["foodlist_picurl"].Trim().ToString();
            foodlistmodel.foodlist_stutas = int.Parse(Request.Form["foodlist_stutas"].Trim().ToString());
            foodlistmodel.foodclass_id = int.Parse(Request.Form["foodclass_id"].Trim().ToString());
            foodlistmodel.foodlist_desp = Request.Form["foodlist_desp"].Trim().ToString();
            foodlistmodel.store_id = int.Parse(user_id);
            foodlistmodel.foodlist_sort = int.Parse(Request.Form["foodlist_sort"].Trim().ToString());

            if (!string.IsNullOrEmpty(Request.Form["id"]))
            {
                bool re = foodlistdal.Update(foodlistmodel);
                if (re)
                {
                    Response.Write("{\"errno\":\"0\",\"tip\":\"保存成功！\",\"url\":\"foodlist.aspx\",\"error\":\"\"}");
                }
                else
                {
                    Response.Write("{\"errno\":\"0\",\"tip\":\"保存失败！\",\"url\":\"foodadd.aspx\",\"error\":\"\"}");
                }
                Response.End();
            }
            else
            {
                foodlistmodel.foodlist_addtime = DateTime.Now;
                int re = foodlistdal.Add(foodlistmodel);
                if (re > 0)
                {
                    Response.Write("{\"errno\":\"0\",\"tip\":\"保存成功！\",\"url\":\"foodlist.aspx\",\"error\":\"\"}");
                }
                else
                {
                    Response.Write("{\"errno\":\"0\",\"tip\":\"保存失败！\",\"url\":\"foodadd.aspx\",\"error\":\"\"}");
                }

                Response.End();
            }
        }

    }

}